package com.hn658.base.message.group.dao.redis.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.hn658.base.message.common.utils.RedisKeyHelper;
import com.hn658.base.message.group.dao.redis.IGroupRedisDAO;
import com.hn658.base.message.group.entity.GroupEO;
import com.hn658.framework.dataaccess.redis.AbstractRedisDAO;
import com.hn658.framework.shared.utils.JsonMapper;


@Component
public class GroupRedisDAOImpl extends AbstractRedisDAO<GroupEO> implements IGroupRedisDAO{

	@Autowired
    @Qualifier("redisTemplate")
    @Override
    public void setTemplate(StringRedisTemplate template) {
        super.template = template;
    }
	
	JsonMapper js = JsonMapper.nonDefaultMapper();

	@Override
	public GroupEO getGroupById(String appId,Long groupId) {
		String groupEOStr = valueOps.get(RedisKeyHelper.groupKeyById(appId,groupId));
		GroupEO gp=null;
		if(StringUtils.isNotBlank(groupEOStr)){
			gp= js.fromJson(groupEOStr, GroupEO.class);
		}
		return gp;
	}

	@Override
	public GroupEO saveGroup(GroupEO gp) {
		if(gp==null){
			return null;
		}
		String groupStr = js.toJson(gp);
		valueOps.set(RedisKeyHelper.groupKeyById(gp.getAppId(),gp.getId()),groupStr);
		return gp;
	}
}
